package com.vhall.component.service.chat.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.chat.QuickRepliesMapper;
import com.vhall.component.entity.chat.dto.QuickRepliesCreateDTO;
import com.vhall.component.entity.chat.dto.QuickRepliesDTO;
import com.vhall.component.entity.chat.entity.QuickRepliesEntity;
import com.vhall.component.entity.chat.enums.QuickRepliesStatusEnum;
import com.vhall.component.entity.chat.enums.QuickRepliesTypeEnum;
import com.vhall.component.entity.chat.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.chat.QuickRepliesAdminService;
import com.vhall.component.service.chat.util.QuickRepliesUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 快捷回复表 服务实现类
 * </p>
 *
 * @author xing.wang
 * @since 2022-09-05
 */
@Service(value = "QuickRepliesAdmin")
@Slf4j
@RequiredArgsConstructor
public class QuickRepliesAdminServiceImpl extends ServiceImpl<QuickRepliesMapper, QuickRepliesEntity> implements QuickRepliesAdminService {

    private final QuickRepliesUtils quickRepliesUtils;

    @Override
    public QuickRepliesCreateDTO create(QuickRepliesCreateVO vo) {
//        验证用户登录信息是否存在
        quickRepliesUtils.checkToken(vo.getToken());
//        验证是否存在重复的快捷回复内容
        if (quickRepliesUtils.checkContentExists(vo.getContent(), 0)) {
            throw new BusinessException(BizErrorCode.COMP_QUICKREPLIES_EXIST_CONTENT_ERROR);
        }
        QuickRepliesEntity entity = new QuickRepliesEntity();
        LocalDateTime now = LocalDateTime.now();
//        admin端用户创建的快捷回复默认设置关联的account_id为0
        entity.setAccountId(0);
        entity.setContent(vo.getContent());
        entity.setUpdatedAt(now);
        entity.setCreatedAt(now);
        entity.setStatus(QuickRepliesStatusEnum.DISABLE.getStatus());
        entity.setType(QuickRepliesTypeEnum.SYSTEM.getType());
        save(entity);
        QuickRepliesCreateDTO quickRepliesDTO = new QuickRepliesCreateDTO();
        BeanUtil.copyProperties(entity, quickRepliesDTO, false);
        return quickRepliesDTO;
    }

    @Override
    public Boolean update(QuickRepliesUpdateVO vo) {
        //        验证用户登录信息是否存在
        quickRepliesUtils.checkToken(vo.getToken());
        if (quickRepliesUtils.checkContentExists(vo.getContent(), vo.getRepliesId())) {
            throw new BusinessException(BizErrorCode.COMP_QUICKREPLIES_EXIST_CONTENT_ERROR);
        }
        QuickRepliesEntity entity = new QuickRepliesEntity();
        entity.setId(vo.getRepliesId());
        entity.setUpdatedAt(LocalDateTime.now());
        entity.setContent(vo.getContent());
        updateById(entity);
        return true;
    }

    @Override
    public Boolean delete(QuickRepliesDeleteVO vo) {
        //        验证用户登录信息是否存在
        quickRepliesUtils.checkToken(vo.getToken());
        return removeById(vo.getRepliesId());
    }

    @Override
    public Boolean setStatus(QuickRepliesSetStatusVO vo) {
        //        验证用户登录信息是否存在
        quickRepliesUtils.checkToken(vo.getToken());
        //        验证开启的快捷回复超过了预期快捷回复,只有设置为开始的时候才做这个验证
        if (QuickRepliesStatusEnum.OPEN.getStatus().toString().equals(vo.getStatus())) {
            quickRepliesUtils.checkQuickRepliesOpenNum(0);
        }
        QuickRepliesEntity entity = new QuickRepliesEntity();
        entity.setUpdatedAt(LocalDateTime.now());
        entity.setId(vo.getRepliesId());
        entity.setStatus(Integer.valueOf(vo.getStatus()));
        updateById(entity);
        return true;
    }

    @Override
    public PageVO<QuickRepliesDTO> pageList(QuickRepliesListVO vo) {
        LambdaQueryWrapper<QuickRepliesEntity> where = Wrappers.lambdaQuery();

        where.eq(StringUtils.isNotEmpty(vo.getStatus()), QuickRepliesEntity::getStatus, vo.getStatus());

        where.eq(StringUtils.isNotEmpty(vo.getType()), QuickRepliesEntity::getType, vo.getType());

        where.like(StringUtils.isNotEmpty(vo.getKeyword()), QuickRepliesEntity::getContent, vo.getKeyword());

        where.orderByDesc(QuickRepliesEntity::getCreatedAt);
        Integer pageIndex = vo.getPage();
        Integer pageSize = vo.getPageSize();
        return quickRepliesUtils.getQuickRepliesDTOPageVO(where, pageIndex, pageSize);
    }

}
